﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using TowerDef_s_enseLibrary.game;

namespace TowerDef_s_enseLibrary.enemy
{
    public class Enemy : Sprite
    {
        #region variables

        protected float startHealth;
        private float _currentHealth;
        /// <summary>
        /// Get and set the current health of the enemy
        /// </summary>
        public float CurrentHealth
        {
            get
            {
                return _currentHealth;
            }
            set
            {
                _currentHealth = value;
            }
        }
        protected float speed = 0.5f;
        public float DistanceToDestination
        {
            get { return Vector2.Distance(position, waypoints.Peek()); }
        }
        /// <summary>
        /// The % of health that an enemy has left
        /// </summary>
        public float HealthPercentage
        {
            get
            {
                return _currentHealth / startHealth;
            }
        }
        private float _modiferCurrentTime;
        private float _speedModifier;
        /// <summary>
        /// Alters the speed of the enemy
        /// </summary>
        public float SpeedModifier
        {
            get
            {
                return _speedModifier;
            }
            set
            {
                _speedModifier = value;
            }
        }
        private float _modifierDuration;
        /// <summary>
        /// Defines how long the speed modification will last
        /// </summary>
        public float ModifierDuration
        {
            get
            {
                return _modifierDuration;
            }
            set
            {
                _modifierDuration = value;
                _modiferCurrentTime = 0;
            }
        }

        protected bool alive = true;
        /// <summary>
        /// Returns that the enemy is death
        /// </summary>
        public bool IsDead
        {
            get { return !alive; }
        }

        private int _bountyGiven;
        /// <summary>
        /// Returns the bounty which you can get from a killed enemy
        /// </summary>
        public int BountyGiven
        {
            get
            {
                return _bountyGiven;
            }
        }

        private Queue<Vector2> waypoints = new Queue<Vector2>();

        #endregion variables

        #region functions

        /// <summary>
        /// Creates a enemy
        /// </summary>
        /// <param name="texture">The Texture of the enemy</param>
        /// <param name="position">The position of the the enemy</param>
        /// <param name="health">The health points of the enemy</param>
        /// <param name="bountyGiven">The bounty of the enemy</param>
        /// <param name="speed">The speed of the enemy</param>
        public Enemy(Texture2D texture, Vector2 position, float health, int bountyGiven, float speed) : base(texture, position)
        {
            this.startHealth = health;
            this._currentHealth = startHealth;
            this._bountyGiven = bountyGiven;
            this.speed = speed;
        }

        /// <summary>
        /// Set Waypoints for the _enemies
        /// </summary>
        /// <param name="waypoints">Waypoints queue</param>
        public void SetWaypoints(Queue<Vector2> waypoints)
        {
            foreach (Vector2 waypoint in waypoints)
                this.waypoints.Enqueue(new Vector2(waypoint.X*32 + 16, waypoint.Y*32 + 48)); // Field = 32 Pixel, 0,0 = 16, 48
            this.position = this.waypoints.Dequeue();
        }

        /// <summary>
        /// Updates the _enemies in matters of the game time
        /// </summary>
        /// <param name="gameTime">The current game time</param>
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (waypoints.Count > 0)
            {
                if (DistanceToDestination < speed)
                {
                    position = waypoints.Peek();
                    waypoints.Dequeue();
                }
                else
                {
                    Vector2 direction = waypoints.Peek() - position;
                    direction.Normalize();

                    // Store the original speed
                    float temporarySpeed = speed;

                    if(_modiferCurrentTime > _modifierDuration)
                    {

                        // If the modifier has finished, reset the modifier.
                        _speedModifier = 0;
                        _modiferCurrentTime = 0;
                    }

                    if(_speedModifier != 0 && _modiferCurrentTime <= _modifierDuration)
                    {
                        // Modify the speed of the enemy
                        temporarySpeed *= _speedModifier;
                        // Update the modifier timer
                        _modiferCurrentTime += (float)gameTime.ElapsedGameTime.TotalSeconds;
                    }

                    velocity = Vector2.Multiply(direction, temporarySpeed);

                    position += velocity;
                }
            }
            else
                alive = false;

            if(_currentHealth <= 0)
                alive = false;
        }

        /// <summary>
        /// Draws the current _enemies
        /// </summary>
        /// <param name="spriteBatch">A group of sprite settings</param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            if (alive)
            {
                base.Draw(spriteBatch, Color.Black);
            }
        }

        #endregion functions

        #region events



        #endregion events
    }
}
